/**
 * Created by zhanglihe on 15/11/10.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'goodsnice',
                condition: {limit: 10, offset: 0},
                model: {
                    event: {
                        '.item-set-goods_nice:click': 'edit'
                    },
                    edit: function(e) {
                        e.preventDefault();
                        var model = this;
                        this.data.goods_intro_num =this.data.intro.length;
                        var nice_id =this.data.nice_id;
                        var html=template('set_goods_nice',this.data);
                        dialog({
                            'content': html,
                            ok: function() {
                                var intro =$("#intro").val();

                                var len = intro.length;
                                if(len < 21 || len > 40){
                                    Alert({msg:'描述在21到40字符之间',type:'error'});
                                    return false;
                                }

                                new Model({
                                    url: _global.url.api + 'home_recommend',
                                    data:{nice_id:nice_id,intro:intro}
                                }).put();
                                _this.collection.reload();
                            }, cancel: function() {}
                        }).show(e.target);
                        $("#intro").on("keyup",function(e){
                            var num =$(this).val().length;
                            if(num<21||num>40){
                                $('.font_num').css({color:'red'})
                                $('.font_num').html(num+'/40');
                            }else{
                                $('.font_num').css({color:'green'})
                                $('.font_num').html(num+'/40');
                            }
                        })
                    }

                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            var _this = this;
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
            $("#btn-add").on("click",function(e){
                e.preventDefault();
                var html=template('goodsadd_tpl');
                dialog({
                    'content': html,
                    ok: function() {
                        var goods =new Array();
                        $("input[name='goods_id']").each(function(e){
                            goods.push($(this).val());
                        });
                        var goods_id =goods.join(',');
                        if(goods_id==''){
                            Alert({msg:'请选择商品',type:'error'});
                            return false;
                        }
                        var intros =new Array();
                        var falg = true;

                        $("#reason_list").find(".intro").each(function(e){
                            if($(this).val()==""){
                                falg =false;
                                return false;
                            }else{
                                falg =true;
                                intros.push($(this).val());
                            }

                        });
                        if(!falg){
                            Alert({msg:'请输入商品描述',type:'error'});
                            return false;
                        }
                        var intro_str =intros.join('*|*');
                        new Model({
                            url: _global.url.api + 'home_recommend',
                            data:{goods_id:goods_id,intro_str:intro_str}
                        }).put();
                        _this.collection.reload();
                    }, cancel: function() {}
                }).show();
                goods_list();
                $("#btn-searchs").on('click',function(e){
                    var keyword =$("#keywords").val();
                    goods_list(keyword)
                });
            });
        }


    };

    var goods_list=function(keyword){
        this.collection = new Collection({
            container: 'data-list_goods',
            template: 'goods_check',
            url: _global.url.api + 'goodsnice_get',
            condition: {keyword:keyword,tab:1},
            model: {
                event: {
                    '.item-select:click': 'goodsnice_set',
                    '.item-select_redy:click':'goodsnice_qx'
                },
                goodsnice_set: function(e) {
                    e.preventDefault();
                    var id =this.data.id;
                    if(!check_goods(id,'goodnice')){
                        return false;
                    };
                    $(".item-selects"+id+"").show();
                    $(".item-select"+id+"").hide();
                    var html =$("#goods_intro").html();
                    $("#reason_list").append("<div id='intro"+id+"'> <form class='form-horizontal' role='form' method='post' style='width:300px;'><input type='hidden' name='goods_id' value='"+id+"'>"+html+"</form>");
                    $("#intro"+id+"").find('.intro').on("keyup",function(e){
                        var num =$(this).val().length;
                        if(num<21||num>40){
                            $("#intro"+id+"").find('.font_num').css({color:'red'})
                            $("#intro"+id+"").find('.font_num').html(num+'/40');
                        }else{
                            $("#intro"+id+"").find('.font_num').css({color:'green'})
                            $("#intro"+id+"").find('.font_num').html(num+'/40');
                        }
                    })
                },
                goodsnice_qx: function(e) {
                    e.preventDefault();
                    var id =this.data.id;
                    $(".item-selects"+id+"").hide();
                    $(".item-select"+id+"").show();
                    $("#intro"+id+"").remove();
                }

            },
            callback: function() {
                loaded();
            }
        });
        this.collection.reload();
    }
    var  check_goods=function(id,status){
        var flag =false;
        $.ajax({
            url: _global.url.api + 'goodsnice_check',
            type: 'get',
            async: false,
            data:{id:id,status:status},
            success: function(res) {
                if(res.code == 0) {
                    flag=true;
                }else{
                    Alert({msg:res.msg,type:'error'});
                    flag=false;
                }
            }
        });
        return flag;
    }

    new index();

});